// pages/SearchDetail/SearchDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        keyword:'',
        typelist:[
            {
                name:'单曲',
                type:1
            },
            {
                name:'专辑',
                type:10
            },
            {
                name:'歌手',
                type:100
            },
            {
                name:'歌单',
                type:1000
            }
        ],
        type:1,
        list:[],
        isLoading:false,
        offset:0
    },
    // 改变搜索种类
    changeType(e){
        this.setData({
            type:e.currentTarget.dataset.index,
            list:[],
            offset:0,
        }) 
        this.getSearchDetail()

    },
    // 获取搜索详情 
    async getSearchDetail(){
        
        this.setData({
            isLoading:true
        })
        wx.showLoading({
          title: 'loading...',
        })
        const res = await wx.p.request({
            url:`${wx.root}/cloudsearch`,
            data:{
                type:this.data.type,
                keywords:this.data.keyword,
                offset:this.data.offset*30
            }
        })
        this.setData({
            offset:this.data.offset+1
        })
        if(this.data.type == 1){
            this.setData({
                list:[...this.data.list,...res.data.result.songs]
            })
        } 
        else if(this.data.type == 10){
            for (let i = 0; i < res.data.result.albums.length; i++) {
                const item = res.data.result.albums[i];
                var a = new Date(item.publishTime);
                item.time = a.toLocaleDateString().replace(/\//g, "-");
            }
            
            this.setData({
                list:[...this.data.list,...res.data.result.albums]
            })
        } 
        else if(this.data.type == 100){
            this.setData({
                list:[...this.data.list,...res.data.result.artists]
            })
        }
        else if(this.data.type == 1000){
            for (let i = 0; i < res.data.result.playlists.length; i++) {
                const item = res.data.result.playlists[i];
                if(item.playCount>10000)
                    item.playCount = parseInt(item.playCount/10000)+'万'
            }
            this.setData({
                list:[...this.data.list,...res.data.result.playlists]
            })
        }
        wx.hideLoading({
          success: (res) => {
              this.setData({
                  isLoading:false
              })
          },
        })
    },
    // 进入详情页
    detail(e){
        const id = e.currentTarget.dataset.id
        if(this.data.type == 1){
            wx.navigateTo({
              url: `/pages/Song/Song?id=${id}`,
            })
        }
        else if(this.data.type == 10){
            wx.navigateTo({
              url: `/pages/Album/Album?id=${id}`,
            })
        }else if(this.data.type == 1000){
            wx.navigateTo({
              url: `/pages/Playlist/Playlist?id=${id}`,
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            keyword:options.keyword
        })
        this.getSearchDetail()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(!this.data.isLoading){
            this.getSearchDetail()
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})